import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    count: 0,
  },
  mutations: {
    add(state) {
      state.count++;
    },
    addN(state, step) {
      state.count += step;
    },
    sub(state) {
      state.count--;
    },
    subN(state, step) {
      state.count -= step;
    },
  },
  actions: {
    addasync(context) {
      setTimeout(() => {
        context.commit("add");
      }, 1000);
    },
    addNasync(context, step) {
      setTimeout(() => {
        context.commit("addN", step); //传参
      }, 1000);
    },
    subasync(context) {
      setTimeout(() => {
        context.commit("sub");
      }, 1000);
    },
    subNasync(context, step) {
      setTimeout(() => {
        context.commit("subN", step);
      }, 1000);
    },
  },
  modules: {},
  getters: {
    show(state) {
      return "当前count：" + state.count;
    },
  },
});
